const router = require('koa-router')()
let list = [{
  chapterId: "1",
  chapterTitle: "我校召开2020年度虚拟仿真实验教学项目培育建设交流会",
  timer: "2023-3-3",
}, {
  chapterId: "2",
  chapterTitle: "我校14项成果获第八届河北省高等教育教学成果奖",
  timer: "2023-3-3",
}, {
  chapterId: "3",
  chapterTitle: "教务处党支部召开支部扩大会议集中学习系列讲话精神",
  timer: "2023-3-3",
},]
router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})
router.get('/getdata', async (ctx, next) => {
  console.log("123")
  ctx.body = "ojbk"
})
router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})
router.get('/getList', async (ctx, next) => {

  ctx.body = list;
})
router.post('/postsub', async (ctx, next) => {
  let chapterTitle = ctx.request.body.chapter
  let chapterObj = {
    chapterId: list.length + 1,
    chapterTitle: chapterTitle,
    timer: "2023-3-3"

  }
  list.push(chapterObj)
  console.log(chapterObj)
  return ctx.body = "ok"
})
router.post("/delchapter", async (ctx, next) => {
  let chapterID = ctx.request.body.chapterId;
  for (let i = 0; i < list.length; i++) {
    if (list[i].chapterId == chapterID) {
      list.splice(i, 1)
      console.log(list)
      ctx.body = 'ok'
      break
    }
  }
  console.log(list)
  return ctx.body = "ok"

})
router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})

module.exports = router
